﻿using EOM.TSHotelManagement.Common.Contract;
using EOM.TSHotelManagement.Common.Core;
using EOM.TSHotelManagement.Common.Util;
using EOM.TSHotelManagement.EntityFramework;

namespace EOM.TSHotelManagement.Application
{
    /// <summary>
    /// 导航控件模块
    /// </summary>
    public class NavBarService : INavBarService
    {
        /// <summary>
        /// 导航控件
        /// </summary>
        private readonly GenericRepository<NavBar> navBarRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="navBarRepository"></param>
        public NavBarService(GenericRepository<NavBar> navBarRepository)
        {
            this.navBarRepository = navBarRepository;
        }

        /// <summary>
        /// 导航控件列表
        /// </summary>
        /// <returns></returns>
        public ListOutputDto<ReadNavBarOutputDto> NavBarList()
        {
            var navBars = navBarRepository.GetList();
            var result = EntityMapper.MapList<NavBar, ReadNavBarOutputDto>(navBars);

            return new ListOutputDto<ReadNavBarOutputDto>
            {
                Data = new PagedData<ReadNavBarOutputDto>
                {
                    Items = result,
                    TotalCount = result.Count
                }
            };
        }

        /// <summary>
        /// 添加导航控件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public BaseResponse AddNavBar(CreateNavBarInputDto input)
        {
            var navBar = new NavBar
            {
                Id = input.Id,
                NavigationBarEvent = input.NavigationBarEvent,
                NavigationBarImage = input.NavigationBarImage,
                NavigationBarName = input.NavigationBarName,
                NavigationBarOrder = input.NavigationBarOrder,
                MarginLeft = input.MarginLeft,
                DataInsUsr = input.DataInsUsr,
                DataInsDate = input.DataInsDate
            };
            var result = navBarRepository.Insert(navBar);
            if (!result)
            {
                return new BaseResponse
                {
                    Code = BusinessStatusCode.InternalServerError,
                    Message = "添加失败"
                };
            }
            return new BaseResponse
            {
                Code = BusinessStatusCode.Success,
                Message = "添加成功"
            };
        }

        /// <summary>
        /// 更新导航控件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public BaseResponse UpdateNavBar(UpdateNavBarInputDto input)
        {
            var navBar = navBarRepository.GetById(input.NavigationBarId);
            if (navBar == null)
            {
                return new BaseResponse
                {
                    Code = BusinessStatusCode.NotFound,
                    Message = "导航控件未找到"
                };
            }
            navBar.NavigationBarName = input.NavigationBarName;
            navBar.NavigationBarOrder = input.NavigationBarOrder;
            navBar.NavigationBarImage = input.NavigationBarImage;
            navBar.NavigationBarEvent = input.NavigationBarEvent;
            navBar.MarginLeft = input.MarginLeft;
            var result = navBarRepository.Update(navBar);
            if (!result)
            {
                return new BaseResponse
                {
                    Code = BusinessStatusCode.InternalServerError,
                    Message = "更新失败"
                };
            }
            return new BaseResponse
            {
                Code = BusinessStatusCode.Success,
                Message = "更新成功"
            };
        }

        /// <summary>
        /// 删除导航控件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public BaseResponse DeleteNavBar(DeleteNavBarInputDto input)
        {
            var navBar = navBarRepository.GetById(input.NavigationBarId);
            if (navBar == null)
            {
                return new BaseResponse
                {
                    Code = BusinessStatusCode.NotFound,
                    Message = "导航控件未找到"
                };
            }
            navBar.IsDelete = 1;
            var result = navBarRepository.SoftDelete(navBar);
            if (!result)
            {
                return new BaseResponse
                {
                    Code = BusinessStatusCode.InternalServerError,
                    Message = "删除失败"
                };
            }
            return new BaseResponse
            {
                Code = BusinessStatusCode.Success,
                Message = "删除成功"
            };
        }
    }
}
